package base.day04;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 描述：
 *      猫狗队列
 * @author hl
 * @version 1.0
 * @date 2020/10/27 8:39
 */
public class Code_07_DogCatQueue {
    public static class Pet {
        private String type;

        public Pet(String type) {
            this.type = type;
        }

        public String getPetType() {
            return this.type;
        }
    }

    public static class Dog extends Pet {
        public Dog() {
            super("dog");
        }
    }

    public static class Cat extends Pet {
        public Cat() {
            super("cat");
        }
    }

    //重新创建一个类封装宠物和时间戳
    public static class PetEnter{
        Pet pet;
        long count;

        public PetEnter(Pet pet, long count) {
            this.pet = pet;
            this.count = count;
        }
        public Pet getPet() {
            return this.pet;
        }

        public long getCount() {
            return this.count;
        }

        public String getEnterPetType() {
            return this.pet.getPetType();
        }
    }

    public static class CatDogQueue{
        private Queue<PetEnter> dogQueue;
        private Queue<PetEnter> catQueue;
        private long count;

        public CatDogQueue() {
            this.dogQueue = new LinkedList<>();
            this.catQueue = new LinkedList<>();
            count = 0;
        }
        public void add(Pet pet){
            if (pet.getPetType().equals("dog")) {
                dogQueue.add(new PetEnter(pet,this.count++));
            }else if (pet.getPetType().equals("cat")) {
                catQueue.add(new PetEnter(pet,this.count++));
            }else{
                throw new RuntimeException("error，no cat or dog");
            }
        }
        //返回猫狗队列的头部先进来的
        public Pet pollAll(){
            if (!catQueue.isEmpty() && !dogQueue.isEmpty()) {
                if (catQueue.peek().getCount() < dogQueue.peek().getCount()) {
                    return catQueue.poll().getPet();
                }else{
                    return dogQueue.poll().getPet();
                }
            }else if (!catQueue.isEmpty()) {
                return catQueue.poll().getPet();
            }else if (!dogQueue.isEmpty()) {
                return dogQueue.poll().getPet();
            }else{
                throw new RuntimeException("both queue is empty");
            }
        }

        public Dog pollDog(){
            if (!dogQueue.isEmpty()) {
                return (Dog) dogQueue.poll().getPet();
            }
            throw new RuntimeException("the dog queue is empty");
        }
        public Cat pollCat(){
            if (!catQueue.isEmpty()) {
                return (Cat) dogQueue.poll().getPet();
            }
            throw new RuntimeException("the cat queue is empty");
        }

        public boolean isEmpty(){return catQueue.isEmpty() && dogQueue.isEmpty();}
        public boolean isDogQueueEmpty(){return dogQueue.isEmpty();}
        public boolean isCatQueueEmpty(){return catQueue.isEmpty();}
    }

    public static void main(String[] args) {
        CatDogQueue test = new CatDogQueue();

        Pet dog1 = new Dog();
        Pet cat1 = new Cat();
        Pet dog2 = new Dog();
        Pet cat2 = new Cat();
        Pet dog3 = new Dog();
        Pet cat3 = new Cat();

        test.add(dog1);
        test.add(cat1);
        test.add(dog2);
        test.add(cat2);
        test.add(dog3);
        test.add(cat3);

        test.add(dog1);
        test.add(cat1);
        test.add(dog2);
        test.add(cat2);
        test.add(dog3);
        test.add(cat3);

        test.add(dog1);
        test.add(cat1);
        test.add(dog2);
        test.add(cat2);
        test.add(dog3);
        test.add(cat3);
//        while (!test.isDogQueueEmpty()) {
//            System.out.println(test.pollDog().getPetType());
//        }
//        while (!test.isEmpty()) {
//            System.out.println(test.pollAll().getPetType());
//        }
        System.out.println(test.pollAll().getPetType());
    }

}
